package com.cqeec.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cqeec.dao.CallRollDao;
import com.cqeec.domain.Ieaved;
import com.cqeec.domain.Student;

public class CallRollDaoImpl implements CallRollDao {
	/**
	 * 查看是否有数据
	 */
	public int findCallRollByStudentName(Connection conn, Student student,String courseNum) throws Exception { 
		int count = 0;
		String sql = "SELECT * FROM call_roll WHERE studentName =? and courseName = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, student.getStudentName());
		psmt.setString(2, courseNum);
		ResultSet rs = psmt.executeQuery();
		while (rs.next()) {
			  count++; 
		}
		return count;
	}
	/**
	 * 如果有就
	 */
	@Override
	public int updateCallRollByStudentName(Connection conn, Student student, String courseNum) throws Exception {
		String sql = "UPDATE call_roll SET courseAbsent = courseAbsent +1 WHERE studentName = ? and courseName = ? ";
		PreparedStatement rs = conn.prepareStatement(sql);
		rs.setString(1, student.getStudentName());
		rs.setString(2, courseNum);
		return rs.executeUpdate();
	}
	/**
	 * 插入数据
	 */
	@Override
	public int persistCallRoll(Connection conn, Student student, String courseNum) throws Exception { 
		
		String sql = "INSERT into call_roll VALUES (?,?,?)";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, student.getStudentName());
		psmt.setString(2, courseNum);
		psmt.setInt(3, 1);
		return psmt.executeUpdate();
	}
	@Override
	public int callroll(Connection con, Ieaved ieaved) {
		int rs=0;
		String sql="INSERT into leaved values (?,?,?,?,?)";
		 try {
			PreparedStatement prst=con.prepareStatement(sql);
			prst.setString(1, ieaved.getCourseName());
			prst.setString(2, ieaved.getClasses());
			prst.setString(3, ieaved.getVacationDate());
			prst.setString(4, ieaved.getVacationCause());
			prst.setString(5, ieaved.getStudentNum());
			 rs=prst.executeUpdate();
		  
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}

}
